package com.yanqu.road.server.manger.activity.newserverwelfare;

import com.yanqu.road.entity.activity.ActivityConditionInfo;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.newserverwelfare.NewServerWelfareReward;
import com.yanqu.road.entity.enums.activity.eNewServerWelfareActivityType;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.enums.eSystemId;
import com.yanqu.road.logic.bussiness.activity.NewServerWelfareBusiness;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.newserverwelfare.NewServerWelfareModule;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.manger.config.SystemOpenMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.utils.date.DateHelper;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

public class NewServerWelfareMgr extends TempMgr {

    public static Map<Integer, ActivityInfo> getActivityInfoMap() {
        return activityInfoMap;
    }

    //活动map
    private static Map<Integer, ActivityInfo> activityInfoMap = new ConcurrentHashMap<>();  //childType
    //剩余奖励数据    <活动小类型<成就ID,剩余奖励信息>>
    private static Map<Integer, Map<Integer, NewServerWelfareReward>> rewardMap = new ConcurrentHashMap<>();

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reload() throws Exception {
        return true;
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    @Override
    public boolean stop() {
        return false;
    }

    @Override
    public boolean save() {
        for (Map.Entry<Integer, Map<Integer, NewServerWelfareReward>> singleMap : rewardMap.entrySet()) {
            Map<Integer, NewServerWelfareReward> singleMapValue = singleMap.getValue();
            for (Map.Entry<Integer, NewServerWelfareReward> newServerWelfareRewardEntry : singleMapValue.entrySet()) {
                NewServerWelfareReward newServerWelfareReward = newServerWelfareRewardEntry.getValue();
                if (newServerWelfareReward.isInsertOption()) {
                    NewServerWelfareBusiness.addNewServerWelfareReward(newServerWelfareReward);
                } else if (newServerWelfareReward.isUpdateOption()) {
                    NewServerWelfareBusiness.upDateNewServerWelfareReward(newServerWelfareReward);
                }
            }

        }
        return true;

    }

    public static ActivityInfo getActivityInfo(int childType) {      //1 在线时间 2 通关数量 3 抽奖次数
        return activityInfoMap.get(childType);
    }

    public static eNewServerWelfareActivityType getChildTypeAttr(int chileType) {
        if (chileType == eNewServerWelfareActivityType.OnlineTime.getValue()) {
            return eNewServerWelfareActivityType.OnlineTime;
        } else if (chileType == eNewServerWelfareActivityType.MissionComplete.getValue()) {
            return eNewServerWelfareActivityType.MissionComplete;
        } else if (chileType == eNewServerWelfareActivityType.DrawTimes.getValue()) {
            return eNewServerWelfareActivityType.DrawTimes;
        }
        return null;
    }

    public static void reloadActivityData() {
        getLogger().info("reload newServerWelfare Activity start");
        //活动信息 ActivityInfo
        List<ActivityInfo> openActivityInfoList = NormalActivityMgr.getOpenActivityInfoList(eActivityType.NewServerWelfareActivity.getValue());
        if (openActivityInfoList.size() == 0) {
            activityInfoMap = new ConcurrentHashMap<>();
            rewardMap = new ConcurrentHashMap<>();
            getLogger().info("no newServerWelfare activity in show time");
            return;
        }
        //eNewServerWelfareActivityType 1累计在线 2关卡通关 3抽奖次数
        for (int i = 1; i <= 3; i++) {
            for (ActivityInfo activityInfo : openActivityInfoList) {
                if (activityInfo.getChildType() == i) {
                    activityInfoMap.put(i, activityInfo);
                    break;
                }
            }
        }
        if (activityInfoMap.get(eNewServerWelfareActivityType.OnlineTime.getValue()) != null) {
            for (GamePlayer player : GamePlayerMgr.getCopyPlayerMap().values()) {
                if (SystemOpenMgr.systemOpen(player, eSystemId.NewServerWelfareActivity.getValue())) {
                    NewServerWelfareModule module = player.getModule(NewServerWelfareModule.class);
                    if (module.getUserOnlineTime() == null || module.getUserOnlineTime().getActivityId() != activityInfoMap.get(eNewServerWelfareActivityType.OnlineTime.getValue()).getActivityId())
                    {
                        module.updateOnlineTime();
                    }
                }
            }
        }

        //读取已开启活动的数据
        for (Map.Entry<Integer, ActivityInfo> entry : activityInfoMap.entrySet()) {
            if ((entry.getKey() == eNewServerWelfareActivityType.MissionComplete.getValue()) || (entry.getKey() == eNewServerWelfareActivityType.DrawTimes.getValue())) {
                //如果是限量领奖的活动,要把全服奖励数据拿出来
                Map<Integer, NewServerWelfareReward> tmpRewardMap = NewServerWelfareBusiness.getNewServerWelfareRewardMap(entry.getValue().getActivityId());
                if (tmpRewardMap.isEmpty()) {
                    //初始化全服奖励
                    tmpRewardMap = initRewardMap(entry.getValue());
                }
                rewardMap.put(entry.getKey(), tmpRewardMap);
            }
        }


    }

    public static Map<Integer, NewServerWelfareReward> initRewardMap(ActivityInfo activityInfo) {
        int activityId = activityInfo.getActivityId();
        Map<Integer, NewServerWelfareReward> singleRewardMap = new ConcurrentHashMap<>();
        List<ActivityConditionInfo> activityConditionInfoList = NormalActivityMgr.getActivityConditionInfoList(activityId);
        for (ActivityConditionInfo activityConditionInfo : activityConditionInfoList) {
            NewServerWelfareReward newServerWelfareReward = new NewServerWelfareReward();
            newServerWelfareReward.setActivityId(activityId);
            newServerWelfareReward.setConditionId(activityConditionInfo.getConditionId());
            newServerWelfareReward.setRewardRemain(activityConditionInfo.getParamList().get(3).intValue());
            newServerWelfareReward.setInsertOption();
            singleRewardMap.put(newServerWelfareReward.getConditionId(), newServerWelfareReward);
        }
        return singleRewardMap;
    }

    public static int getActivityHadOpenDay() {
        ActivityInfo activityInfo = activityInfoMap.get(eNewServerWelfareActivityType.OnlineTime.getValue());
        if (activityInfo != null) {
            return DateHelper.calc2DateTDOADays(new Date(activityInfo.getBeginTime() * 1000), new Date());
        }
        return 0;
    }

    public static void updateAllUserOnlineTime() {

        ActivityInfo activityInfo = activityInfoMap.get(eNewServerWelfareActivityType.OnlineTime.getValue());
        if (activityInfo == null) {
            return;
        }
        if (ActivityMgr.activityInTime(activityInfo)) {
            for (GamePlayer player : GamePlayerMgr.getAllOnlinePlayer()) {
                if (player != null) {
                    player.getModule(NewServerWelfareModule.class).updateOnlineTime();
                }
            }
        }
    }

    public static boolean checkActivityOntime(int smallType) {
        if (!activityInfoMap.containsKey(smallType)) {
            return false;
        }
        ActivityInfo activityInfo = activityInfoMap.get(smallType);
        if (activityInfo == null) {
            return false;
        }
        if (!ActivityMgr.activityInTime(activityInfo)) {
            return false;
        }
        return true;
    }

    public static Map<Integer, Map<Integer, NewServerWelfareReward>> getRewardCopyMap() {
        return new ConcurrentHashMap<Integer, Map<Integer, NewServerWelfareReward>>(rewardMap);
    }

    public static Map<Integer, Map<Integer, NewServerWelfareReward>> getRewardMap() {
        return rewardMap;
    }
}
